import {shallowReactive} from "vue";



export const instances: any[] = shallowReactive([])


export const getInstance = (id: string) => {
    const idx = instances.findIndex((instance) => instance.id === id)
    const current = instances[idx]
    let prev: any | undefined
    if(idx > 0){
        prev = instances[idx - 1]
    }
    return {
        current,
        prev
    }
}


export const getLastOffset = (id: string): number => {
    const {prev} = getInstance(id)
    if(!prev) return 0
    return prev.vm.exposed!.bottom.value
}

/**
 * 获取偏移或者是间距
 * 如果该元素位于头部那么就是 offset
 * 否则就是 间距 16
 * @param id
 * @param offset
 */
export const getOffsetOrSpace = (id: string, offset: number) => {
    const idx = instances.findIndex((instance) => instance.id === id)
    return idx > 0 ? 16 : offset
}
